package com.xframework.security.config;

import org.directwebremoting.servlet.DwrListener;
import org.directwebremoting.servlet.DwrServlet;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.Ordered;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xuning on 2021/2/5.
 */
@Configuration
@ImportResource(locations = {"classpath:dwr-spring-config.xml"})
public class DwrConf {

    @Bean
    public ServletListenerRegistrationBean registDwrListener() {
        System.out.println("------------------------- registDwrListener ---------------------------");
        ServletListenerRegistrationBean<DwrListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<DwrListener>();
        servletListenerRegistrationBean.setListener(new DwrListener());
        servletListenerRegistrationBean.setEnabled(Boolean.TRUE);
        servletListenerRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return servletListenerRegistrationBean;
    }

    @Bean
    public ServletRegistrationBean registDwrServlet() {
        System.out.println("------------------------- registDwrServlet ---------------------------");
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("debug", "true");
        initParameters.put("accessLogLevel", "runtimeexception");
        initParameters.put("activeReverseAjaxEnabled", "true");
        initParameters.put("initApplicationScopeCreatorsAtStartup", "true");
        initParameters.put("jsonRpcEnabled", "true");
        initParameters.put("jsonpEnabled", "true");
        initParameters.put("preferDataUrlSchema", "false");
        ServletRegistrationBean<DwrServlet> servletRegistrationBean = new ServletRegistrationBean<DwrServlet>();
        servletRegistrationBean.setServlet(new DwrServlet());
        servletRegistrationBean.setName("dwr-invoker");
        servletRegistrationBean.setInitParameters(initParameters);
        servletRegistrationBean.addUrlMappings("/dwr/*");
        servletRegistrationBean.setEnabled(Boolean.TRUE);
        servletRegistrationBean.setLoadOnStartup(Ordered.HIGHEST_PRECEDENCE);
        return servletRegistrationBean;
    }

}
